﻿<!DOCTYPE html>
<!--[if lt IE 7]>      <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
<!--[if IE 7]>         <html class="no-js lt-ie9 lt-ie8"> <![endif]-->
<!--[if IE 8]>         <html class="no-js lt-ie9"> <![endif]-->
<!--[if gt IE 8]><!-->
<html class="no-js">
<!--<![endif]-->

<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>企业微信管理后台</title>
    <meta name="description" content="">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="../../css/reset.css">
    <link rel="stylesheet" href="../../css/fonts.css">
    <link rel="stylesheet" href="../../js/bootstrap/css/bootstrap.min.css">
    <!-- bootstrap表格 -->
    <link rel="stylesheet" href="../../js/bootstrap/js/bootstrap-table-master/bootstrap-table.min.css">
    <link rel="stylesheet" href="../../js/layui/css/layui.css">
    <link rel="stylesheet" href="../../css/main.css">
</head>

<!--新建群发任务/0310-->

<body>
    <div id="setup_msg">
        <div class="header">
            <div class="type_select_wrapper">
                <ul class="type_ul">
                    <li>新建群发任务</li>
                </ul>
            </div>
        </div>
        <div class="setup_msg_container">
            <div class="type_select_wrapper" id="Tips"></div>
            <!-- 2020-03-16 start -->
            <div class="msg_times">本周群发任务次数：5次，点击<span class="on_msg_detail">查看详情</span></div>
            <div class="tips_wrapper"><span class="tips_text"><i class="iconfont icon-zhuyi"></i>注意：同一个企业对一个客户一个自然周内（周一至周日）至多只能发送一条消息，超过限制的用户将会被忽略</span></div>
            <!-- 2020-03-16 end -->
            <div class="msg_wrapper">
                <div class="send_cus" id="send_cus">选择发送的客户</div>
                <div class="send_cus sended_cus" id="sended_cus">
                    分别发送给：<span class="sended_type"></span><span class="sended_edit" id="update_select">修改</span>
                </div>
                <div class="send_cus sended_cus" id="fail_cus">
                    筛选失败的客户：<span id="faildata"></span><span class="sended_edit" id="fail_edit">查看详情</span>
                </div>
                <div class="variable_wrapper">
                    <span class="variable_text">插入变量：</span>
                    <div class="variable_itemlist" id="variable_itemlist">

                    </div>
                </div>
                <div class="text_container">
                    <div class="textarea_box">
                        <textarea class="textarea" id="msg" placeholder="输入消息内容..." maxlength="4000"></textarea>
                    </div>
                </div>
                <div class="addImage">
                    <div class="add_image_wrapper">
                        <i class="iconfont icon-ic_add"></i>
                        <span>添加图片</span>
                        <input class="upload_image_input" type="file" accept="image">
                    </div>
                    <div class="image_show_wrapper" id="image_show_wrapper">
                    </div>
                </div>
                <div class="send_btn_container">
                    <a class="normal_btn send_btn" href="javascript:;" id="notice_send">通知成员发送</a>
                    <span class="send_desc">通知成员，向选中的客户发送以上企业消息</span>
                </div>
            </div>
        </div>
    </div>
    <!-- 新建消息模态框 -->
    <div class="modal fade" id="setup_modal" tabindex="-1" role="dialog" aria-labelledby="setup_modal_label">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                    <h4 class="modal-title" id="setup_modal_label">选择客户</h4>
                </div>
                <div class="modal-body">
                    <form class="layui-form">
                        <div class="send_wrapper">
                            <div class="send_item">
                                <div class="left_text select_type_text">选择方式</div>
                                <div class="right_select">
                                    <div class="layui-form-item">
                                        <div class="layui-input-block">
                                            <input type="radio" lay-filter="send_type" name="type" value="Excel导入"
                                                title="Excel导入">
                                            <input type="radio" lay-filter="send_type" name="type" value="全部客户"
                                                title="全部客户" checked>
                                            <input type="radio" lay-filter="send_type" name="type" value="按条件筛选的客户"
                                                title="按条件筛选的客户">
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <!-- excel导入 -->
                            <div class="send_item screen_excel_import" id="screen_excel_import" style="display: none;">
                                <div class="excel_wrapper">
                                    <div class="select_excel_box">
                                        <div class="select_text">选择文件</div>
                                        <input class="excel_input" id="excel_input" name="excel_input" type="file"
                                            accept="application/vnd.ms-excel,application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet">
                                    </div>
                                    <span class="input_file_name" style="display: none;"></span>
                                    <a class="download_template" href="javascript:;">下载模板</a>
                                </div>
                            </div>

                            <!-- 按条件筛选的客户 -->
                            <div class="send_item screen_send_item" id="screen_condition" style="display: none;">
                                <div class="left_text">筛选条件</div>
                                <div class="right_select">
                                    <div class="layui-form-item">
                                        <div class="layui-input-block">
                                            <div class="add_text" id="addperoson_modal_show">选择添加人</div>
                                        </div>
                                        <div class="selectedMembers" style="display: none;">
                                            <div class="add_logo">已选择</div>
                                            <div class="selected_members member_items" id="selected_dep_members">

                                            </div>
                                        </div>

                                        <div class="add_logo">标签</div>
                                        <div class="layui-input-block" id="addTag">

                                        </div>
                                        <div class="send_tip" style="display: none;">
                                            将发送消息给<span class="first_selected_member"></span>等 <span
                                                class="selected_number"></span> 人添加的客户
                                        </div>
                                        <div class="send_tip_total" style="display: none;">
                                            将发送消息给<span class="first_selected_member"></span>等 <span
                                                class="selected_number"></span> 人添加的，且满足“<span
                                                class="selected_logo"></span>”等标签的客户
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-primary" id="submit_send_msg">确定</button>
                    <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                </div>
            </div>
        </div>
    </div>
    <!-- 新建消息模态框 -->
    <!-- 添加人模态框 -->
    <div class="modal fade" id="addperson_modal" tabindex="-1" role="dialog" aria-labelledby="addperson_modal_label">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                    <h4 class="modal-title" id="addperson_modal_label">选择发送范围</h4>
                </div>
                <div class="modal-body">
                    <div class="select_person_wrapper">
                        <div class="left_wrapper">
                            <div class="protree_container"></div>
                        </div>
                        <div class="right_wrapper">
                            <div class="title">已选择的部门或成员</div>
                            <div class="selected">
                                <div class="department_members selected_department" id="selected_department">

                                </div>
                                <div class="department_members selected_members" id="selected_members">

                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-primary" id="submit_dep_member">确定</button>
                    <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                </div>
            </div>
        </div>
    </div>
    <!-- 添加人模态框 -->
    <!-- 发送失败客户详情模态框 -->
    <div class="modal fade" id="fail_detail" tabindex="-1" role="dialog" aria-labelledby="msg_detail_label">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                </div>
                <div class="modal-body">
                    <div class="msg_container">
                        <input type="hidden" class="group_record_id" value="">
                        <div class="send_info">
                            <div class="send_head">
                                <span id="send_num">筛选失败的客户</span>
                            </div>
                            <div class="send_detail">
                                <table class="layui-table" style="table-layout: fixed;">
                                    <thead>
                                        <tr>
                                            <th class="wechatName">客户微信名</th>
                                            <th class="name">客户姓名</th>
                                            <th class="send_person">发送人</th>
                                            <th class="state">筛选失败原因</th>
                                        </tr>
                                    </thead>
                                    <tbody id="tby_details">
                                    </tbody>
                                </table>
                                <div class="paging_style" id="send_list_paging"></div>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-primary" data-dismiss="modal">确定</button>
                </div>
            </div>
        </div>
    </div>
    <!-- 发送失败客户详情模态框 -->

    <div class="mask-bg">
        <div class="mask-loading">
            <div class="sk-spinner sk-spinner-cube-grid">
                <div class="iconfont icon-jiazai"></div>
            </div>
            <div class="tip">加载中</div>
        </div>
    </div>

    <script src="../../js/Base.js"></script>
    <script src="../../js/jquery-3.3.1.min.js"></script>
    <script src="../../js/bootstrap/js/bootstrap.min.js"></script>
    <!-- bootstrap表格 -->
    <script src="../../js/bootstrap/js/bootstrap-table-master/bootstrap-table.min.js"></script>
    <script src="../../js//bootstrap/js/bootstrap-table-master/bootstrap-table-zh-CN.min.js"></script>
    <script src="../../js/layui/layui.js"></script>
    <!-- 添加人数据 -->
    <script src="../../js/Base.js"></script>
    <!-- 树形结构 -->
    <script src="../../js/proTree.js"></script>
    <!-- 压缩图片 -->
    <script src="../../js/exif.js"></script>
    <!-- 上传图片 -->
    <script src="../../js/addPerson_data/upload-image.js"></script>
    <script>
        var Domain = "http://apichat.vaubang.cn"
        var Authorization = "CMRYkkhFCZvssxZenSQfdfwidNmRSp1kG7kHntncTwYPX778uJ6u38Ct32xnPtC1jVD8HPqXLklGS0bCUb3vNf9bSrbyF4-GYHuRmjEfU9eTZEBst3Ye8bdiMJmPA6zxnA2KSGZhRDx1i9UvR1cWAMyPFeiXoNoukvA3Z0LYxFzdAMopMTJNJU4MDTB784tuz9ykgFNIlLmmq9XXkFI_Jg"
        //下载导入消息任务客户模板
        $(".download_template").click(function () {
            var url = Domain + '/EXPORTTemplate?FileName=101';
            var xhr = new XMLHttpRequest();
            xhr.open('Get', url, true);        // 也可以使用POST方式，根据接口
            xhr.responseType = "blob";    // 返回类型blob
            xhr.onload = function () {
                if (this.status === 200) {
                    var blob = this.response;
                    var reader = new FileReader();
                    reader.readAsDataURL(blob);    // 转换为base64，可以直接放入a表情href
                    reader.onload = function (e) {
                        var a = document.createElement('a');
                        a.download = '消息任务客户模板.xlsx';//下载文件名
                        a.href = e.target.result;
                        $("body").append(a);  // 修复firefox中无法触发click
                        a.click();
                        $(a).remove();
                    }
                }
            };
            // 发送ajax请求
            xhr.send()
        });
        //后台传入的 标题列表
        var arr = [];
        // 切换navtab
        $(".type_ul li a").click(function () {
            $(this)
                .addClass("active")
                .parent()
                .siblings("li")
                .find("a")
                .removeClass("active");
            navIndex = parseInt($(this).attr("data-index"));
            if (navIndex == 0) {
                $(".msg_wrapper").show();
                $(".group_record").hide();
            } else {
                $(".msg_wrapper").hide();
                $(".group_record").show();
            }
        });
        layui.use("form", function () {
            var form = layui.form;
            let send_type = "全部客户"; // 发送类型--默认全部客户
            let send_logo = []; // 筛选发送客户的标签的名字
            let send_logoId = []; //筛选发送客户的标签的id
            let External_userid = []; //筛选发送的客户id
            let Execl_userid = []; //execl中筛选发送的客户id
            var Sender = [];  //筛选发送客户的成员id
            $(".mask-bg").show();
            // 返回
            $(".back").click(function () {
                self.location.href = "cus-contact.html?nav_index=" + nav_index;
            });
            //查询本周发送消息任务的客户情况
            $.ajax({
                url: Domain + "/GetGroupMsgTaskUser",
                type: "get",
                beforeSend: function (request) {
                    request.setRequestHeader("Authorization", Authorization);
                },
                success: function (data) {
                    if (data) {
                        if (data.Response.Code == 200) {
                            var datainfo = data.Response.Data;
                            $("#Tips").empty();
                            if (datainfo.SendNum == 0) {
                                $("#Tips").html("您本周还未发下达过群发任务！");
                            }
                            else {
                                var EndSendUrl = "<a title='点击查看本周发送详情' id='send_detail'>" + datainfo.EndTime + "</a>";
                                var sureSendUrl = datainfo.SureSendNum + "名客户";
                                if (sureSendUrl != 0) {
                                    sureSendUrl = "<a title='点击查看详情' id='sure_detail'>" + datainfo.SureSendNum + "名客户</a>";
                                }
                                $("#Tips").html("您本周已下达" + datainfo.SendNum + "次群发任务，最近一次在" + EndSendUrl + "，本周还可以给" + sureSendUrl + "发送群发消息");
                                $("#send_detail").click(function () {
                                    self.location.href = "group-msg-history.html?startTime=" + datainfo.WeekStartTime
                                });
                                $("#sure_detail").click(function () {
                                    self.location.href = "../data-statistics/customer-list.html?page=../cus-contact/setup-msg"
                                });
                            }
                            form.render();
                        }
                        else {
                            FindLoginState(data.Response.Msg, "GetGroupMsgTaskUser");
                        }
                    }
                },
                error: function (err) {
                    console.log(err)
                }
            })
            // 筛选类型
            form.on("radio(send_type)", function (data) {
                if (data.value == "按条件筛选的客户") {
                    $("#screen_condition").show();
                    $("#screen_excel_import").hide();
                } else if (data.value == "Excel导入") {
                    $("#screen_condition").hide();
                    $("#screen_excel_import").show();
                } else {
                    $("#screen_excel_import").hide();
                    $("#screen_condition").hide();
                }
                send_type = data.value;
                //console.log("send_type", send_type)
            });
            // 筛选标签的选择与取消
            form.on("checkbox(send_logo)", function (data) {
                if (data.elem.checked == true) {
                    send_logo.push(data.elem.title);
                    send_logoId.push(data.value);
                } else {
                    for (let i = 0; i < send_logo.length; i++) {
                        if (send_logo[i] == data.elem.title) {
                            send_logo.splice(i, 1);
                        }
                    }
                    for (let i = 0; i < send_logoId.length; i++) {
                        if (send_logoId[i] == data.value) {
                            send_logoId.splice(i, 1);
                        }
                    }
                }
                if (send_logo.length > 0 && totalDatalist.length > 0) {
                    $(".selected_logo").text([...send_logo]);
                    $(".send_tip_total").show()
                    $(".send_tip").hide()
                } else if (send_logo.length == 0 && totalDatalist.length > 0) {
                    $(".send_tip_total").hide()
                    $(".send_tip").show()
                } else {
                    $(".send_tip_total").hide()
                    $(".send_tip").hide()
                }
            });
            // 点击添加人 打开添加模态框 且 重新加载已选择的成员
            $("#addperoson_modal_show").click(function () {
                $("#addperson_modal").modal("show");
                //console.log(depSelected, memberSelected)
                let html1 = "";
                let html2 = "";
                for (let i = 0; i < depSelected.length; i++) {
                    html1 += `
                                                <div class="dep_item">
                                                    <i class="iconfont icon-wenjianjia"></i>
                                                    <span data-id="${depSelected[i].id}" data-pid="${depSelected[i].pid}" data-dep="${depSelected[i].department}">${depSelected[i].name}</span>
                                                    <i class="iconfont icon-shanchu delete_dep_mem"></i>
                                                </div>
                                                `;
                }
                for (let j = 0; j < memberSelected.length; j++) {
                    html2 += `
                                                <div class="mem_item">
                                                    <i class="iconfont icon-geren"></i>
                                                    <span data-id="${memberSelected[j].id}" data-pid="${memberSelected[j].pid}" data-dep="${memberSelected[j].department}">${memberSelected[j].name}</span>
                                                    <i class="iconfont icon-shanchu delete_dep_mem"></i>
                                                </div>
                                                `;
                }
                $("#selected_department").html(html1);
                $("#selected_members").html(html2);
            });
            $("#send_cus").click(function () {
                $("#setup_modal").modal("show");
            });
            $("#update_select").click(function () {
                $("#setup_modal").modal("show");
            });
            let depSelected = []; // 选择的部门
            let memberSelected = []; // 选择的成员
            let totalDatalist = []; // 部门、成员总集合
            //请求拿到部门和成员数据
            $.ajax({
                url: Domain + "/FindDepartmentAndMember",
                type: "get",
                beforeSend: function (request) {
                    request.setRequestHeader("Authorization", Authorization);
                },
                success: function (res) {
                    if (res) {
                        if (res.Response.Code == 200) {
                            var data = res.Response.Data;
                            //console.log(data);
                            for (var i = 0; i < data.length; i++) {
                                var datainfo = {
                                    id: data[i].Id,
                                    name: data[i].Name,
                                    pid: data[i].PId,
                                    department: data[i].Department,
                                    Useful: data[i].Useful
                                };
                                arr.push(datainfo);
                            }
                            //标题的图标是集成bootstrap 的图标  更改 请参考bootstrap的字体图标替换自己想要的图标
                            $(".protree_container").ProTree({
                                arr: arr,
                                close: false,
                                simIcon: "iconfont icon-geren", //单个标题字体图标 不传默认glyphicon-file
                                mouIconOpen: "iconfont icon-wenjianjia1", //含多个标题的打开字体图标  不传默认glyphicon-folder-open
                                mouIconClose: "iconfont icon-wenjianjia", //含多个标题的关闭的字体图标  不传默认glyphicon-folder-close
                                callback: function (id, name, pid, department, useful) {
                                    let data = {
                                        id,
                                        name,
                                        pid: parseInt(pid),
                                        department,
                                        Useful: useful
                                    };
                                    if (department == true || department == 'true') {
                                        data.id = parseInt(id)
                                        data.department = true
                                    } else if (department == 'false') {
                                        data.department = false
                                    }
                                    if (useful == 'true') {
                                        data.Useful = true
                                    } else if (useful == 'false') {
                                        data.Useful = false
                                    }
                                    //console.log(data);
                                    let isAdd = true;
                                    if (memberSelected.length > 0) {
                                        for (let i = 0; i < memberSelected.length; i++) {
                                            if (memberSelected[i].name == data.name) {
                                                isAdd = false
                                                layer.msg('已经选过此成员了')
                                                break
                                            }
                                        }
                                    }
                                    if (depSelected.length > 0) {
                                        for (let i = 0; i < depSelected.length; i++) {
                                            if (depSelected[i].name == data.name) {
                                                isAdd = false
                                                layer.msg('已经选过此部门了')
                                                break
                                            }
                                        }
                                    }
                                    if (isAdd) {
                                        if (data.department) {
                                            depSelected.push(data);
                                            $("#selected_department").append(`
                                                    <div class="dep_item">
                                                        <i class="iconfont icon-wenjianjia"></i>
                                                        <span data-id="${data.id}" data-pid="${data.pid}" data-dep="${data.department}" data-Useful="${data.Useful}">${data.name}</span>
                                                        <i class="iconfont icon-shanchu delete_dep_mem"></i>
                                                    </div>
                                                    `);
                                        } else {
                                            memberSelected.push(data);
                                            $("#selected_members").append(`
                                                    <div class="mem_item">
                                                        <i class="iconfont icon-geren"></i>
                                                        <span data-id="${data.id}" data-pid="${data.pid}" data-dep="${data.department}" data-Useful="${data.Useful}">${data.name}</span>
                                                        <i class="iconfont icon-shanchu delete_dep_mem"></i>
                                                    </div>
                                                    `);
                                        }
                                    }

                                }
                            });
                            // 拿到需要发送的部门、成员列表
                            $("#submit_dep_member").click(function () {
                                totalDatalist = [];
                                totalDatalist = totalDatalist.concat(depSelected, memberSelected);
                                let html = "";
                                for (let i = 0; i < totalDatalist.length; i++) {
                                    html += `
                                                                <div class="member_item">
                                                                    <i class="iconfont ${
                                        totalDatalist[i].department == "true"
                                            ? "icon-wenjianjia"
                                            : "icon-geren"
                                        }"></i>
                                                                    <span data-id="${totalDatalist[i].id}" data-dep="${
                                        totalDatalist[i].department
                                        }" data-pid="${totalDatalist[i].pid}" data-Useful="${totalDatalist[i].Useful}">${totalDatalist[i].name}</span>
                                                                    <i class="iconfont icon-shanchu delete_dep_mem"></i>
                                                                </div>
                                                            `;
                                }
                                $("#selected_dep_members").html(html);
                                $(".selected_number").text(totalDatalist.length);
                                if (totalDatalist.length > 0) {
                                    $(".first_selected_member").text(totalDatalist[0].name)
                                    if (send_logo.length > 0) {
                                        $(".selected_logo").text([...send_logo]);
                                        $(".send_tip_total").show()
                                    }
                                    else {
                                        $(".send_tip").show()
                                    }
                                }
                                $("#addperson_modal").modal("hide");
                                $(".selectedMembers").show();
                            });
                        }
                        else {
                            FindLoginState(res.Response.Msg, "FindDepartmentAndMember");
                        }
                    }
                },
                error: function (err) {
                    console.log(err)
                }
            })
            // 删除部门、成员
            $(".department_members").on("click", ".delete_dep_mem", function () {
                let id = $(this)
                    .siblings("span")
                    .attr("data-id");
                let dep = $(this)
                    .siblings("span")
                    .attr("data-dep");
                $(this)
                    .parent()
                    .remove();
                if (dep == "true" || dep == true) {
                    for (let i = 0; i < depSelected.length; i++) {
                        if (id == depSelected[i].id) {
                            depSelected.splice(i, 1);
                            break;
                        }
                    }
                } else if (dep == "false" || dep == false) {
                    for (let i = 0; i < memberSelected.length; i++) {
                        if (id == memberSelected[i].id) {
                            memberSelected.splice(i, 1);
                            break;
                        }
                    }
                }
                //console.log(memberSelected, depSelected);
            });
            // 删除已选择部门、成员
            $(".selected_members").on("click", ".delete_dep_mem", function () {
                let id = $(this)
                    .siblings("span")
                    .attr("data-id");
                let dep = $(this)
                    .siblings("span")
                    .attr("data-dep");
                $(this)
                    .parent()
                    .remove();
                if (dep == "true" || dep == true) {
                    for (let i = 0; i < depSelected.length; i++) {
                        if (id == depSelected[i].id) {
                            depSelected.splice(i, 1);
                            break;
                        }
                    }
                } else if (dep == "false" || dep == false) {
                    for (let i = 0; i < memberSelected.length; i++) {
                        if (id == memberSelected[i].id) {
                            memberSelected.splice(i, 1);
                            break;
                        }
                    }
                }
                for (let j = 0; j < totalDatalist.length; j++) {
                    if (id == totalDatalist[j].id) {
                        totalDatalist.splice(j, 1);
                        break;
                    }
                }
                // 2020-02-25 start
                if (totalDatalist.length == 0) {
                    $(".selectedMembers").hide();
                    $(".send_tip_total").hide();
                    $(".send_tip").hide()
                }
                else {
                    $(".first_selected_member").text(totalDatalist[0].name)
                    $(".selected_number").text(totalDatalist.length);
                }
                // 2020-02-25 end
                //console.log(memberSelected, depSelected);
            });
            //获取所有标签
            $.ajax({
                url: Domain + "/FindCorpTagList1",
                type: "get",
                beforeSend: function (request) {
                    request.setRequestHeader("Authorization", Authorization);
                },
                success: function (data) {
                    $(".mask-bg").hide();
                    if (data) {
                        if (data.Response.Code == 200) {
                            var datainfo = data.Response.Data;
                            $("#addTag").empty();
                            $.each(datainfo, function (i) {
                                for (var x = 0; x < datainfo[i].Tag.length; x++) {
                                    $("#addTag").append(
                                        '<input type="checkbox" lay-filter="send_logo" name="logo" lay-skin="primary" value="' + datainfo[i].Tag[x].Id + '" title="' + datainfo[i].Tag[x].Name + '">');
                                }
                            });
                            form.render();
                        }
                        else {
                            FindLoginState(data.Response.Msg, "FindCorpTagList1");
                        }
                    }
                },
                error: function (err) {
                    console.log(err)
                }
            })
            //请求拿到字段数据
            $.ajax({
                url: Domain + "/GetMessageFieldList",
                type: "get",
                beforeSend: function (request) {
                    request.setRequestHeader("Authorization", Authorization);
                },
                success: function (res) {
                    if (res) {
                        if (res.Response.Code == 200) {
                            var data = res.Response.Data;
                            //console.log("data_cc",data)
                            var css = "";
                            for (var i = 0; i < data.length; i++) {
                                css += '<div class="item" data-text="' + data[i].FiedText + '">' + data[i].FieldType + '</div>';
                            }
                            $("#variable_itemlist").html(css);
                        }
                        else {
                            FindLoginState(res.Response.Msg, "GetMessageFieldList");
                        }
                    }
                },
                error: function (err) {
                    console.log(err)
                }
            })
            // excel导入客户
            $("#excel_input").on('change', function (e) {
                var fileObj = e.currentTarget.files[0]; // js 获取文件对象
                if (typeof (fileObj) == "undefined" || fileObj.size <= 0) {
                    alert("请选择文件");
                    return;
                }
                $(".input_file_name").show()
                $(".input_file_name").text(fileObj.name)
                $(".input_file_name").attr('title', fileObj.name)
                let formData = new FormData();
                formData.append("fileType_file", fileObj);
                var index = layer.load(1, {
                    shade: [0.1, '#fff'] //0.1透明度的白色背景
                });
                $.ajax({
                    url: Domain + "/AddExcelByShop?filetype=GroupMsgCustomer",
                    type: "POST",
                    data: formData,
                    cache: false,
                    processData: false,
                    contentType: false,
                    beforeSend: function (request) {
                        request.setRequestHeader("Authorization", Authorization);
                    },
                    success: function (res) {
                        if (res) {
                            if (res.Response.Code == 200) {
                                var data = res.Response.Data;
                                $.ajax({
                                    url: Domain + "/AnalysisSubmitData",
                                    type: "post",
                                    beforeSend: function (request) {
                                        request.setRequestHeader("Authorization", Authorization);
                                    },
                                    data: {
                                        Type: 'GroupMsgCustomer', File: data.Path
                                    },
                                    success: function (result) {
                                        if (result) {
                                            if (result.Response.Code == 200) {
                                                Execl_userid = result.Response.Data;
                                                console.log("Execl_userid", Execl_userid)
                                                layer.close(index)
                                                $("#excel_input").val("");
                                                layer.msg('导入成功')
                                            }
                                            else {
                                                $("#excel_input").val("");
                                                layer.close(index)
                                                FindLoginState(result.Response.Msg, "AnalysisSubmitData")
                                            }
                                        }
                                    },
                                    error: function (e) {
                                        console.log(e.status);
                                        console.log(e.responseText);
                                    }
                                });
                            }
                            else {
                                //window.alert("上传失败" + res.Response.Msg);
                                layer.msg("上传失败," + res.Response.Msg);
                            }
                        }
                    },
                    error: function (e) {
                        console.log(e.status);
                        console.log(e.responseText);
                    }
                });
            });
            //获取部门成员
            function getMember(depMember, depList) {
                if (depList && depList.length > 0) {
                    for (let i = 0; i < depList.length; i++) {
                        if (depList[i].department == true || depList[i].department == 'true') {
                            let m = []
                            for (let j = 0; j < arr.length; j++) {
                                if (arr[j].pid == depList[i].id) {
                                    m.push(arr[j])
                                }
                            }
                            getMember(depMember, m)
                        } else {
                            if (depList[i].Useful == true || depList[i].Useful == 'true') {
                                depMember.push(depList[i])
                            }
                        }
                    }
                }
            }
            //展示筛选客户中 无效的客户
            function postFailList(data) {
                $("#faildata").text("共计" + data.FailDataList.length + "名客户");
                $("#fail_cus").show();
                $("#fail_edit").click(function () {
                    $("#fail_detail").modal("show");
                    var css = ""; var result = data.FailDataList;
                    console.log()
                    var endlength = 4;
                    if (endlength > result.length) {
                        endlength = result.length;
                    }
                    $("#tby_details").empty();
                    for (var i = 0; i < endlength; i++) {
                        var state = '<td class="state fail">' + result[i].FailReason + '</td>';
                        css += '<tr>' +
                            '<td class="wechatName">' + result[i].CustomerWeChatName + '</td>' +
                            '<td class="name">' + result[i].CustomerName + '</td>' +
                            '<td class="send_person">' + result[i].SenderName + '</td>' +
                            state +
                            '</tr>';
                    }
                    $("#tby_details").html(css);
                    layui.use('form', function () {
                        var form = layui.form;
                        form.render();
                    });
                    $("#send_num").html("总计" + data.FailDataList.length + "名客户)");
                    // 表格分页
                    layui.use('laypage', function () {
                        var laypage = layui.laypage;
                        laypage.render({
                            elem: 'send_list_paging',
                            limit: 5,
                            theme: '#E51C23',
                            count: data.TotalRecords, //数据总数，从服务端得到
                            jump: function (obj, first) {
                                Page_id = obj.curr;
                                Page_size = obj.limit;
                                //首次不执行
                                if (!first) {
                                    var css = ""; var result = data.FailDataList;
                                    var startlength = (obj.curr - 1) * obj.limit;
                                    var endlength = obj.curr * obj.limit;
                                    if (endlength > result.length) {
                                        endlength = result.length;
                                    }
                                    $("#tby_details").empty();
                                    for (var i = startlength; i < endlength; i++) {
                                        var state = '<td class="state fail">' + result[i].FailReason + '</td>';
                                        css += '<tr>' +
                                            '<td class="wechatName">' + result[i].CustomerWeChatName + '</td>' +
                                            '<td class="name">' + result[i].CustomerName + '</td>' +
                                            '<td class="send_person">' + result[i].SenderName + '</td>' +
                                            state +
                                            '</tr>';
                                    }
                                    $("#tby_details").html(css);
                                    layui.use('form', function () {
                                        var form = layui.form;
                                        form.render();
                                    });
                                }
                            }
                        })
                    });
                });
            }
            // 确认发送给的客户
            $("#submit_send_msg").click(function () {
                External_userid = [];
                if (send_type == "Excel导入") {
                    var data = Execl_userid;
                    if (data.FailDataList.length > 0) {
                        postFailList(data);
                    }
                    if (data.SucessDataList.length == 0) {
                        $("#setup_modal").modal("hide");
                        $(".sended_type").text(send_type + "(共计0名客户)");
                        $("#sended_cus").show();
                        $("#send_cus").hide();
                        layer.msg("没有满足查询条件的用户");
                        return false;
                    }
                    for (var i = 0; i < data.SucessDataList.length; i++) {
                        External_userid.push(new {
                            "External_userid": data.SucessDataList[i].External_userid,
                            "Sender": data.SucessDataList[i].Sender
                        });
                    };
                    $("#setup_modal").modal("hide");
                    $(".sended_type").text(send_type + "(共计" + data.SucessDataList.length + "名客户)");
                    $("#sended_cus").show();
                    $("#send_cus").hide();
                }
                else if (send_type == "按条件筛选的客户") {
                    // 取出选中的部门中的所有成员
                    let depMember = []
                    Sender = []
                    getMember(depMember, totalDatalist)
                    // 成员去重
                    for (var i = 0; i < depMember.length; i++) {
                        for (var j = i + 1; j < depMember.length; j++) {
                            if (depMember[i].id == depMember[j].id) {
                                depMember.splice(j, 1);
                            }
                        }
                    }
                    //console.log('depMember', depMember)
                    if (depMember.length == 0) {
                        layer.msg("请选择成员");
                        return false;
                    }
                    for (var i = 0; i < depMember.length; i++) {
                        Sender.push(depMember[i].id);
                    }
                    let dataList = {
                        "TagId": send_logoId,
                        "UserId": Sender,
                    }
                    //console.log("dataList", dataList);
                    //通过标签和用户获取客户
                    $.ajax({
                        url: Domain + "/GetTaskCorpTagCustomerList",
                        type: "post",
                        data: dataList,
                        beforeSend: function (request) {
                            request.setRequestHeader("Authorization", Authorization);
                        },
                        success: function (res) {
                            if (res) {
                                if (res.Response.Code == 200) {
                                    var data = res.Response.Data;
                                    if (data.FailDataList.length > 0) {
                                        postFailList(data);
                                    }
                                    if (data.SucessDataList.length == 0) {
                                        $("#setup_modal").modal("hide");
                                        $(".sended_type").text(send_type + "(共计0名客户)");
                                        $("#sended_cus").show();
                                        $("#send_cus").hide();
                                        layer.msg("没有满足查询条件的用户");
                                        return false;
                                    }
                                    for (var i = 0; i < data.SucessDataList.length; i++) {
                                        External_userid.push(new {
                                            "External_userid": data.SucessDataList[i].External_userid,
                                            "Sender": data.SucessDataList[i].Sender
                                        });
                                    };
                                    $("#setup_modal").modal("hide");
                                    $(".sended_type").text(send_type + "(共计" + data.SucessDataList.length + "名客户)");
                                    $("#sended_cus").show();
                                    $("#send_cus").hide();
                                }
                                else {
                                    layer.msg("没有满足查询条件的用户");
                                    $("#setup_modal").modal("hide");
                                    $(".sended_type").text(send_type + "(共计0名客户)");
                                    $("#sended_cus").show();
                                    $("#send_cus").hide();
                                }
                            }
                        },
                        error: function (err) {
                            console.log(err)
                        }
                    })
                }
                else {
                    //查询企业全部成员的客户
                    $.ajax({
                        url: Domain + "/GetTaskCustomerList",
                        type: "get",
                        beforeSend: function (request) {
                            request.setRequestHeader("Authorization", Authorization);
                        },
                        success: function (res) {
                            if (res) {
                                if (res.Response.Code == 200) {
                                    var data = res.Response.Data;
                                    if (data.FailDataList.length > 0) {
                                        postFailList(data);
                                    }
                                    if (data.SucessDataList.length == 0) {
                                        $("#setup_modal").modal("hide");
                                        $(".sended_type").text(send_type + "(共计0名客户)");
                                        $("#sended_cus").show();
                                        $("#send_cus").hide();
                                        layer.msg("没有满足查询条件的用户");
                                        return false;
                                    }
                                    for (var i = 0; i < data.SucessDataList.length; i++) {
                                        External_userid.push(new {
                                            "External_userid": data.SucessDataList[i].External_userid,
                                            "Sender": data.SucessDataList[i].Sender
                                        });
                                    };
                                    $("#setup_modal").modal("hide");
                                    $(".sended_type").text(send_type + "(共计" + data.SucessDataList.length + "名客户)");
                                    $("#sended_cus").show();
                                    $("#send_cus").hide();
                                }
                                else {
                                    layer.msg("没有满足查询条件的用户");
                                    $("#setup_modal").modal("hide");
                                    $(".sended_type").text(send_type + "(共计0名客户)");
                                    $("#sended_cus").show();
                                    $("#send_cus").hide();
                                }
                            }
                        },
                        error: function (err) {
                            console.log(err)
                        }
                    })
                }
            });
            upload_image(".upload_image_input", "#image_show_wrapper"); // 第一个参数为上传图片的input类名，第二个为图片显示的类名
            // 删除上传的图片
            $(".image_show_wrapper").on("click", ".image_delete", function () {
                let image_src = $(this).prev().attr('src') // 取到图片的路径
                $.ajax({
                    url: Domain + "/RemoveFile?filePath=" + image_src,
                    type: "get",
                    beforeSend: function (request) {
                        request.setRequestHeader("Authorization", Authorization);
                    },
                    success: function (result) {
                        if (result) {
                            if (result.Response.Code == 200) {
                                // 当ajax请求完成 图片成功删除后 执行以下操作
                                ImgMediaId = "";
                                $(this).parent().remove();
                            }
                            else {
                                FindLoginState(result.Response.Msg, "RemoveFile");
                            }
                        }
                    },
                    error: function (e) {
                        console.log(e.status);
                        console.log(e.responseText);
                    }
                });
            });
            // 通知成员发送
            $("#notice_send").click(function () {
                //console.log("External_userid", External_userid);
                let msg = $("#msg").val()
                if (msg == null || msg == "") {
                    layer.msg("请填写发送消息文本");
                    return false;
                }
                if (External_userid == null || External_userid == "") {
                    layer.msg("请选择要发送的客户");
                    return false;
                }
                let dataList = {
                    "External_userid": External_userid,
                    "Text": {
                        "Content": msg,
                    },
                    "Image": {
                        "Media_id": ImgMediaId,
                    },
                };
                if (ImgMediaId == null || ImgMediaId == "") {
                    dataList = {
                        "External_userid": External_userid,
                        "Text": {
                            "Content": msg,
                        },
                    };
                }
                //console.log("AddMessageTask_postdata",dataList);
                $.ajax({
                    url: Domain + "/AddMessageTask",
                    type: "post",
                    data: dataList,
                    beforeSend: function (request) {
                        request.setRequestHeader("Authorization", Authorization);
                    },
                    success: function (res) {
                        if (res) {
                            if (res.Response.Code == 200) {
                                ImgUrl = ""; //添加的图片服务器路径
                                ImgMediaId = ""; //添加的图片Media_id
                                window.location.reload();
                                window.alert("生成成功");
                            }
                            else {
                                FindLoginState(res.Response.Msg, "AddMessageTask");
                            }
                        }
                    },
                    error: function (err) {
                        console.log(err)
                    }
                })
            });
            //选择字段
            $(".variable_itemlist").on('click', '.item', function () {
                let oldValue = $("#msg").val();
                let newValue = oldValue + $(this).attr('data-text');
                $("#msg").val(newValue);
            })
            // 2020-03-16 start
            // 页面跳转
            $(".on_msg_detail").click(function () {
                parent.location.href = "cus-contact.html?nav_index=1"
            })
            // 2020-03-16 end
        });
    </script>
</body>

</html>